package com.situ.b2c.catalog.controller;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoader;

import com.situ.b2c.catalog.service.CatalogService;
import com.situ.b2c.pojo.Catalog;


@WebServlet("/CatalogAddServlet")
public class CatalogAddServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private CatalogService catalogService;
   private Catalog catalog;
	
	
	
	public CatalogAddServlet() {
		ApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
		catalogService= context.getBean(CatalogService.class);
		catalog = context.getBean(Catalog.class);
		
	}
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String parentId = request.getParameter("parentId");
		String cataName = request.getParameter("cataName");
		String rankCount = request.getParameter("rankCount");
		String activeFlag = request.getParameter("activeFlag");
		String createDate = request.getParameter("createDate");
		System.out.println(createDate);
		String createBy = request.getParameter("createBy");
		
		
		
		catalog.setParentId(Long.parseLong(parentId));
		catalog.setCataName(cataName);
		catalog.setRankCount(Integer.parseInt(rankCount));
		catalog.setActiveFlag(Integer.parseInt(activeFlag));
		catalog.setCreateBy(createBy);
		try {
			catalog.setCreateDate(new SimpleDateFormat("yyyy-MM-dd").parse(createDate));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		
		
		catalogService.insert(catalog);
		response.sendRedirect("CatalogSelectServlet");
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}
